#!/bin/bash

# Prometheus 容器单独启动脚本
# Prometheus Container Startup Script

set -e

echo "📈 启动 Prometheus 容器..."
echo "================================"

# 检查 Docker 和 Docker Compose
if ! command -v docker &> /dev/null; then
    echo "❌ Docker 未安装，请先安装 Docker"
    exit 1
fi

if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
    echo "❌ Docker Compose 未安装，请先安装 Docker Compose"
    exit 1
fi

# 进入项目根目录
cd "$(dirname "$0")/.."

echo "📁 当前目录: $(pwd)"

# 检查 docker-compose.yml 是否存在
if [ ! -f "docker-compose.yml" ]; then
    echo "❌ docker-compose.yml 不存在"
    exit 1
fi

echo "🚀 单独启动 Prometheus 容器..."

# 使用新版本的 docker compose 或旧版本的 docker-compose
if docker compose version &> /dev/null; then
    COMPOSE_CMD="docker compose"
else
    COMPOSE_CMD="docker-compose"
fi

# 只启动 Prometheus 服务
$COMPOSE_CMD up -d prometheus

echo "⏳ 等待 Prometheus 服务启动..."
sleep 10

# 检查服务状态
echo "📊 检查服务状态..."
$COMPOSE_CMD ps prometheus

echo "✅ Prometheus 容器启动完成！"
echo "📈 Prometheus: http://localhost:9090"
echo "📊 Metrics: http://localhost:9090/metrics"
echo "🎯 Targets: http://localhost:9090/targets"
echo ""
echo "📄 查看日志: $COMPOSE_CMD logs -f prometheus"
echo "🛑 停止容器: $COMPOSE_CMD stop prometheus"
echo "🗑️  删除容器: $COMPOSE_CMD down prometheus"